package fr.blogger.ejb.model;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.eclipse.persistence.annotations.Index;

@Entity
@Table(name = "blog")
@TableGenerator(name = "SEQUENCE_BLOG", initialValue = 0, table = "sequence", pkColumnName = "code", valueColumnName = "valeur", allocationSize = 1)
public class Blog implements Serializable {

	@Id
	@Column(name = "id")
	@GeneratedValue(strategy = GenerationType.TABLE, generator = "SEQUENCE_BLOG")
	private Integer id;

	@Index
	@Size(max = 100)
	@NotNull
	@Column(name = "nom", length = 100, nullable = false)
	private String nom;

	@Size(max = 4000)
	@NotNull
	@Column(name = "description", length = 4000, nullable = false)
	private String description;

	@Size(max = 1000)
	@Column(name = "mots_cles", length = 1000, nullable = true)
	private String motsCles;

	@NotNull
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "date_creation", nullable = false)
	private Date dateCreation;

	@NotNull
	@ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
	@JoinColumn(name = "ref_utilisateur_createur", nullable = false)
	private Utilisateur utilisateurCreateur;

	@OneToMany(cascade = CascadeType.REFRESH, mappedBy = "blog")
	public List<Article> listeArticle;

	@ManyToMany(mappedBy = "listeBlogAbonne", cascade = {CascadeType.REMOVE, CascadeType.MERGE})
	private List<Utilisateur> listeUtilisateurAbonne;

	public Blog() {
		super();
	}

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getNom() {
		return this.nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getMotsCles() {
		return motsCles;
	}

	public void setMotsCles(String motsCles) {
		this.motsCles = motsCles;
	}

	public Date getDateCreation() {
		return dateCreation;
	}

	public void setDateCreation(Date dateCreation) {
		this.dateCreation = dateCreation;
	}

	public Utilisateur getUtilisateurCreateur() {
		return utilisateurCreateur;
	}

	public void setUtilisateurCreateur(Utilisateur utilisateurCreateur) {
		this.utilisateurCreateur = utilisateurCreateur;
	}

	public List<Article> getListeArticle() {
		return listeArticle;
	}

	public void setListeArticle(List<Article> listeArticle) {
		this.listeArticle = listeArticle;
	}

	public List<Utilisateur> getListeUtilisateurAbonne() {
		return listeUtilisateurAbonne;
	}

	public void setListeUtilisateurAbonne(List<Utilisateur> listeUtilisateurAbonne) {
		this.listeUtilisateurAbonne = listeUtilisateurAbonne;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (obj == this) {
			return true;
		}
		if (obj.getClass() != getClass()) {
			return false;
		}
		Blog other = (Blog) obj;
		return new EqualsBuilder().append(id, other.id).isEquals();
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder(17, 37).append(id).toHashCode();
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this).append("id", id).toString();
	}

}
